#!/bin/bash
PROJECT_NAME=`ls -thrl|grep jar|awk '{print $NF}'`

declare -A A 	
for SERVER in ${PROJECT_NAME[@]}    
do    
   # 跳过被注释的数据库    
    echo $SERVER | grep -E '^#' >/dev/null 2>&1    
    if [ "$?" -eq 0 ]    
    then    
        continue    
    fi    

    array=($(echo $SERVER | tr ',' ' '))    
    A['JAR_NAME']=${array[0]}

isExist(){
 #首先查找进程号
     pid=`ps -ef | grep ${A['JAR_NAME']} | grep -v grep | grep -v kill| awk '{print $2}'`
  #如果进程号不存在，则返回0 否则返回1
    if [ -z "${pid}" ]; then
     return 0
    else
     return 1
    fi
}

## 编写启动程序方法
start(){
 #调用 判断程序是否正在运行的方法
  #isExist
    #判断方法返回值是否等于0 ，等于则不存在
    if [ $? -eq "0" ]; then
     echo "${A['JAR_NAME']} is starting ......"
     nohup java -jar -Dspring.profiles.active=prod ${A['JAR_NAME']} >/dev/null  2>&1 &
     echo "${A['JAR_NAME']} startup success"
    else
     echo "${A['JAR_NAME']} is running, pid=${pid} "
    fi
}

#编写停止程序的方法
stop(){
    #调用 判断程序是否正在运行
    isExist
    #判断是否存在，返回值0不存在
    if [ $? -eq "0" ]; then
     echo "${A['JAR_NAME']} is not running ......"
    else
     echo "${A['JAR_NAME']} is running, pid=${pid}, prepare kill it "
     kill -9 ${pid}
     echo "${A['JAR_NAME']} has been successfully killed ......"
    fi
}


#编写重启方法
restart(){
    ## 先停止再启动
    stop
    start
}


case "$1" in
 "start")
  start
  ;;
 "stop")
  stop
  ;;
 "restart")
  restart
  ;;
 *)
  echo "请类似这样执行 ./*.sh start or ./*sh restart"
  echo "case:sh run.sh [start | stop | restart | status]"
  ;;
esac

	
done